<template>
  <ele-modal
    form
    :width="420"
    :model-value="modelValue"
    :title="title"
    @closed="closed"
    @update:model-value="updateModelValue"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="审核状态" prop="status">
        <el-radio-group v-model="form.status" placeholder="请选择审核状态">
          <el-radio
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="!remarkDisabled" label="审核备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="2"
          placeholder="请输入审核备注（选填）"
          autocomplete="off"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="updateModelValue(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">确定</el-button>
    </template>
  </ele-modal>
</template>

<script setup>
const props = defineProps({
  /** 弹窗是否打开 */
  modelValue: Boolean,
  title: {
    type: String,
    default: '审核订单',
  },
  /**  是否需要填写备注 */
  remarkDisabled: Boolean,
  statusList: {
    type: Array,
    default: () => [
      {
        value: 'YS',
        label: '通过',
      },
      {
        value: 'NS',
        label: '不通过',
      },
    ],
  },
})

const emit = defineEmits(['done', 'update:modelValue'])

const formRef = ref(null)

const loading = ref(false)

/** 表单数据 */
const [form, resetFields] = utils.useFormData({
  status: '',
  remark: props.remarkDisabled ? undefined : '',
})

/** 表单验证规则 */
const rules = reactive({
  status: [
    {
      required: true,
      message: '请选择审核状态',
      trigger: 'change',
    },
  ],
})

/** 更新modelValue */
function updateModelValue(value) {
  emit('update:modelValue', value)
}

function save() {
  formRef.value?.validate?.((valid) => {
    if (!valid)
      return
    loading.value = true
    emit('done', form, () => loading.value = false)
  })
}

function closed() {
  resetFields()
  formRef.value?.clearValidate?.()
}
</script>

<style lang="less" scoped></style>
